library(ggplot2) ## msleep is included in ggplot-2
library(plotly)

Attaching package: ‘plotly’

The following object is masked from ‘package:ggplot2’:

    last_plot

The following object is masked from ‘package:stats’:

    filter

The following object is masked from ‘package:graphics’:

    layout
library(data.table)
data.table 1.14.2 using 1 threads (see ?getDTthreads).  Latest news: r-datatable.com
**********
This installation of data.table has not detected OpenMP support. It should still work but in single-threaded mode.
This is a Mac. Please read https://mac.r-project.org/openmp/. Please engage with Apple and ask them for support. Check r-datatable.com for updates, and our Mac instructions here: https://github.com/Rdatatable/data.table/wiki/Installation. After several years of many reports of installation problems on Mac, it's time to gingerly point out that there have been no similar problems on Windows or Linux.
**********
df = msleep
print(paste("Attributes of the data-set"))
[1] "Attributes of the data-set"
colnames(df)
 [1] "name"         "genus"        "vore"         "order"        "conservation" "sleep_total" 
 [7] "sleep_rem"    "sleep_cycle"  "awake"        "brainwt"      "bodywt"      
str(df)
tibble [83 × 11] (S3: tbl_df/tbl/data.frame)
 $ name        : chr [1:83] "Cheetah" "Owl monkey" "Mountain beaver" "Greater short-tailed shrew" ...
 $ genus       : chr [1:83] "Acinonyx" "Aotus" "Aplodontia" "Blarina" ...
 $ vore        : chr [1:83] "carni" "omni" "herbi" "omni" ...
 $ order       : chr [1:83] "Carnivora" "Primates" "Rodentia" "Soricomorpha" ...
 $ conservation: chr [1:83] "lc" NA "nt" "lc" ...
 $ sleep_total : num [1:83] 12.1 17 14.4 14.9 4 14.4 8.7 7 10.1 3 ...
 $ sleep_rem   : num [1:83] NA 1.8 2.4 2.3 0.7 2.2 1.4 NA 2.9 NA ...
 $ sleep_cycle : num [1:83] NA NA NA 0.133 0.667 ...
 $ awake       : num [1:83] 11.9 7 9.6 9.1 20 9.6 15.3 17 13.9 21 ...
 $ brainwt     : num [1:83] NA 0.0155 NA 0.00029 0.423 NA NA NA 0.07 0.0982 ...
 $ bodywt      : num [1:83] 50 0.48 1.35 0.019 600 ...

1) Scatter Plot

scatter_plot = plot_ly(data=msleep, x=~(-log10(bodywt)), y=~(-log10(brainwt)),  color = ~name,
                       type='scatter',mode='markers') %>%
                       layout(
                          title= list(text = "<b>Body weight vs Brain weight"),
                          legend = list(title = list(text ='<b>Animals')), 
                          xaxis  = list(title = list(text ='<b>Brain Weight')),
                          yaxis  = list(title = list(text ='<b>Body Weight')))
scatter_plot
Warning: Ignoring 27 observations
Warning in RColorBrewer::brewer.pal(N, "Set2") :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

Warning in RColorBrewer::brewer.pal(N, "Set2") :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

Warning: Ignoring 27 observations
Warning in RColorBrewer::brewer.pal(N, "Set2") :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

Warning in RColorBrewer::brewer.pal(N, "Set2") :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

2) Barchart

head(df)

2) a) Regular Barchart

r_barchart = plot_ly(data=df, y=~order, type="bar") %>%
                  layout(
                          title= list(text = "<b>Total sleep time of Animals based on Vore"),
                          legend = list(title = list(text= '<b>Vore')),
                          xaxis  = list(title = list(text ='<b>sleep total', tickformat = "%")),
                          yaxis  = list(title = list(text ='<b>Count')))
r_barchart

2) b) Stacked Barchart

r_stack_barchart = data.table::melt(df, id.vars='vore') %>%
plot_ly(x = ~vore, y = ~value, type = 'bar', name = ~variable, color = ~variable) %>%
      layout(
          title= list(text = "<b>Total Distribution based on Vore"),
          legend = list(title = list(text= '<b>Aniamal Feature')),
          xaxis  = list(title = list(text ='<b>Vores')),
          yaxis = list(title='Count', text='<b>Count'), barmode = 'stack')
Warning in data.table::melt(df, id.vars = "vore") :
  The melt generic in data.table has been passed a tbl_df and will attempt to redirect to the relevant reshape2 method; please note that reshape2 is deprecated, and this redirection is now deprecated as well. To continue using melt methods from reshape2 while both libraries are attached, e.g. melt.list, you can prepend the namespace like reshape2::melt(df). In the next version, this warning will become an error.
r_stack_barchart
Warning: Ignoring 184 observations
Warning in RColorBrewer::brewer.pal(N, "Set2") :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

Warning in RColorBrewer::brewer.pal(N, "Set2") :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

Warning: Ignoring 184 observations
Warning in RColorBrewer::brewer.pal(N, "Set2") :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

Warning in RColorBrewer::brewer.pal(N, "Set2") :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

2) c) Grouped Barchart

r_group_barchart = data.table::melt(df, id.vars='vore') %>%
plot_ly(x = ~vore, y = ~value, type = 'bar', name = ~variable, color = ~variable) %>%
      layout(
          title= list(text = "<b>Total Distribution based on Vore"),
          legend = list(title = list(text= '<b>Aniamal Feature')),
          xaxis  = list(title = list(text ='<b>Vores')),
          yaxis = list(title='Count', text='<b>Count'), barmode = 'group')
Warning in data.table::melt(df, id.vars = "vore") :
  The melt generic in data.table has been passed a tbl_df and will attempt to redirect to the relevant reshape2 method; please note that reshape2 is deprecated, and this redirection is now deprecated as well. To continue using melt methods from reshape2 while both libraries are attached, e.g. melt.list, you can prepend the namespace like reshape2::melt(df). In the next version, this warning will become an error.
r_group_barchart
Warning: Ignoring 184 observations
Warning in RColorBrewer::brewer.pal(N, "Set2") :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

Warning in RColorBrewer::brewer.pal(N, "Set2") :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

Warning: Ignoring 184 observations
Warning in RColorBrewer::brewer.pal(N, "Set2") :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

Warning in RColorBrewer::brewer.pal(N, "Set2") :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

3) Histogram

# Define labels for the bars
labs = c('herbi'='Herbivore',
         'carni'='Carnivore',
         'omni'='Omnivore',
         'insecti'='Insectivore')
histogram_plot = plot_ly(data = df, x = ~sleep_total, name=~vore,type="histogram") %>%
                  layout(
                          title= list(text = "<b>Total sleep time of Animals based on Vore"),
                          legend = list(title = list(text= '<b>Vore')),
                          xaxis  = list(title = list(text ='<b>sleep total')),
                          yaxis  = list(title = list(text ='<b>Count')))
histogram_plot

4)Pie-Chart

df_order = data.frame(table(msleep$order))
df_order
pie_chart = plot_ly(type='pie', labels=df_order$Var1, values=df_order$Freq, 
                    textinfo='label+percent',insidetextorientation='radial') %>%
                    layout(
                          title= list(text = "<b>Order Distributions"),
                          legend = list(title = list(text= '<b>Order')))

pie_chart

5) Donut Chart / Open Pie-Chart

df_vore = data.frame(table(msleep$vore))
df_vore
donut_chart = plot_ly(labels=df_vore$Var1, values=df_vore$Freq, 
                    textinfo='label+percent') %>%
                    add_pie(hole = 0.6) %>%
                    layout(
                          title= list(text = "<b>Order Distributions"),
                          legend = list(title = list(text= '<b>Order')))

donut_chart 

6) Box Plot

box_plot=plot_ly(data=msleep, y=~sleep_total, color=~vore, type='box') %>%
          layout(
                title= list(text = "<b>Total sleep time Distribution in Vore Basis"),
                legend = list(title = list(text= '<b>Vore')),
                xaxis  = list(title = list(text ='<b>Vore')),
                yaxis  = list(title = list(text ='<b>Sleep Total Distribution')))
box_plot
LS0tCnRpdGxlOiAiMTlNSUQwMDIwIFBsb3RzIgpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sKLS0tCgpgYGB7cn0KbGlicmFyeShnZ3Bsb3QyKSAjIyBtc2xlZXAgaXMgaW5jbHVkZWQgaW4gZ2dwbG90LTIKbGlicmFyeShwbG90bHkpCmxpYnJhcnkoZGF0YS50YWJsZSkKYGBgCgpgYGB7cn0KZGYgPSBtc2xlZXAKYGBgCgoKYGBge3J9CnByaW50KHBhc3RlKCJBdHRyaWJ1dGVzIG9mIHRoZSBkYXRhLXNldCIpKQpjb2xuYW1lcyhkZikKYGBgCgoKYGBge3J9CnN0cihkZikKYGBgCgojIyAxKSBTY2F0dGVyIFBsb3QKYGBge3J9CnNjYXR0ZXJfcGxvdCA9IHBsb3RfbHkoZGF0YT1tc2xlZXAsIHg9figtbG9nMTAoYm9keXd0KSksIHk9figtbG9nMTAoYnJhaW53dCkpLCAgY29sb3IgPSB+bmFtZSwKICAgICAgICAgICAgICAgICAgICAgICB0eXBlPSdzY2F0dGVyJyxtb2RlPSdtYXJrZXJzJykgJT4lCiAgICAgICAgICAgICAgICAgICAgICAgbGF5b3V0KAogICAgICAgICAgICAgICAgICAgICAgICAgIHRpdGxlPSBsaXN0KHRleHQgPSAiPGI+Qm9keSB3ZWlnaHQgdnMgQnJhaW4gd2VpZ2h0IiksCiAgICAgICAgICAgICAgICAgICAgICAgICAgbGVnZW5kID0gbGlzdCh0aXRsZSA9IGxpc3QodGV4dCA9JzxiPkFuaW1hbHMnKSksIAogICAgICAgICAgICAgICAgICAgICAgICAgIHhheGlzICA9IGxpc3QodGl0bGUgPSBsaXN0KHRleHQgPSc8Yj5CcmFpbiBXZWlnaHQnKSksCiAgICAgICAgICAgICAgICAgICAgICAgICAgeWF4aXMgID0gbGlzdCh0aXRsZSA9IGxpc3QodGV4dCA9JzxiPkJvZHkgV2VpZ2h0JykpKQpzY2F0dGVyX3Bsb3QKYGBgCiMjIDIpIEJhcmNoYXJ0CgpgYGB7cn0KaGVhZChkZikKYGBgCgojIyMgMikgYSkgUmVndWxhciBCYXJjaGFydApgYGB7cn0Kcl9iYXJjaGFydCA9IHBsb3RfbHkoZGF0YT1kZiwgeT1+b3JkZXIsIHR5cGU9ImJhciIpICU+JQogICAgICAgICAgICAgICAgICBsYXlvdXQoCiAgICAgICAgICAgICAgICAgICAgICAgICAgdGl0bGU9IGxpc3QodGV4dCA9ICI8Yj5Ub3RhbCBzbGVlcCB0aW1lIG9mIEFuaW1hbHMgYmFzZWQgb24gVm9yZSIpLAogICAgICAgICAgICAgICAgICAgICAgICAgIGxlZ2VuZCA9IGxpc3QodGl0bGUgPSBsaXN0KHRleHQ9ICc8Yj5Wb3JlJykpLAogICAgICAgICAgICAgIC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